<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      class="post-btn"
      :disabled="message.length === 0"
      @click="submit"
      >发布</van-button
    >
  </div>
</template>

<script>
import { addCommentInfoApi } from "@/api/Article";
export default {
  name: "CommentPost",
  props: {
    // 文章ID
    targetId: {
      type: [Number, String],
      required: true,
    },
    type: {
      type: String,
      default: "a",
    },
  },
  // 注入祖先提供的数据
  inject: ["articleId"],
  data() {
    return {
      message: "",
    };
  },
  methods: {
    async submit() {
      if (!this.$store.getters.token) return this.$toast.fail("请登录");
      try {
        let { data } = await addCommentInfoApi({
          target: this.targetId,
          content: this.message,
          art_id: this.type === "a" ? null : this.articleId,
        });
        this.$emit("postSuccess", data.data.new_obj);
        this.message = "";
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
